﻿namespace IntellectShop.Common
{
	using System.Linq;

	public partial class CartItem
	{
		public static Common.CartItem RetrieveByID(int id)
		{
			using (DB.IntellectShopDataContext context = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				var cartItemDB = context.CartItems.SingleOrDefault(item => item.CartItemID == id);
				if (cartItemDB == null)
					return null;

				return new Common.CartItem
				{
					CartItemID = cartItemDB.CartItemID,
					Commodity = Common.Commodity.RetrieveByID(cartItemDB.CommodityID),
					Count = cartItemDB.Count,
					Created = cartItemDB.Created
				};
			}
		}
	}
}
